package com.ssm.changeunit.console.parser;

import com.ssm.changeunit.command.*;
import org.apache.commons.cli.*;

/**
 * 
 * @author 시리우스
 */
public class RenameConsoleParser extends ConsoleParser {
    @Override
    public CommandWorkerOptions parse(java.lang.String[] userCommand) throws ConsoleParseException {
        try {
            // 파서 생성
            CommandLineParser parser = new GnuParser();

            Options options = new Options();

            // Optional
            Option originOption = OptionBuilder.withLongOpt("origin").hasArg().create();

            // Required
            Option destOption = OptionBuilder.withLongOpt("dest").hasArg().create();
            
            options.addOption(originOption);
            options.addOption(destOption);

            CommandLine commandLine = parser.parse(options, userCommand);

            CommandWorkerOptions commandWorkerOptions = new CommandWorkerOptions();
            
            // origin 프로퍼티가 있는지 검사
            if(commandLine.hasOption("origin")) {
                CommandWorkerOption commandWorkerOption = new CommandWorkerOption("origin");
                
                commandWorkerOptions.addCommandWorkerOption(commandWorkerOption);
                
                String item = commandLine.getOptionValue("origin");

                commandWorkerOption.addValue(item);
            }
            
            // dest 프로퍼티가 있는지 검사
            if(commandLine.hasOption("dest")) {
                CommandWorkerOption commandWorkerOption = new CommandWorkerOption("dest");
                
                commandWorkerOptions.addCommandWorkerOption(commandWorkerOption);
                
                String item = commandLine.getOptionValue("dest");

                commandWorkerOption.addValue(item);
            }
            else {
                // dest 프로퍼티는 Required이므로,
                // 없으면 예외를 발생시킨다.
                throw new ConsoleParseException("dest 프로퍼티가 존재하지 않습니다.");
            }
            
            return commandWorkerOptions;
        }
        catch(ParseException ex) {
            throw new ConsoleParseException(ex);
        }
    }
}

